package com.zy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.Post;
import com.zy.mapper.ZyPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 杨深俊
 * @Date: 2021/04/14/18:56
 * @Description:
 */
@Controller
public class ZyPostController {
    @Autowired
    ZyPostMapper zyPostMapper;

    @RequestMapping("/postManage")
    public ModelAndView getPosts(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("PostManage");

        //分页
        IPage<Post> postIPage = new Page<>(pageNum, pageSize);
        IPage<Post> posts = zyPostMapper.selectPage(postIPage, null);
        modelAndView.addObject("posts", posts);
        return modelAndView;
    }

    //添加职位
    @PostMapping("addPost")
    public String addPost(@ModelAttribute Post post, Integer pageNum) {
        zyPostMapper.insert(post);
        return "redirect:postManage?pageNum=" + pageNum;
    }

    //删除职位
    @RequestMapping("/delPost")
    public String delPost(Integer id) {
        zyPostMapper.delPosts(id);
        zyPostMapper.delPost(id);
        return "redirect:postManage";
    }

    //修改职位
    @PostMapping("updatePost")
    public String updatePost(@ModelAttribute Post post, Integer pageNum) {
        zyPostMapper.updatePost(post);
        return "redirect:postManage?pageNum=" + pageNum;
    }

    @GetMapping("getPost")
    @ResponseBody
    public Post getPost(Integer id) {
        Post post = zyPostMapper.getPost(id);
        System.out.println(post);
        return post;
    }
}
